package com.mellarius.Adaptor;

import android.widget.ArrayAdapter;






import com.helper.RevisionsHlp;

import java.util.ArrayList;
import java.util.List;
import com.mellarius.R;
import com.mellarius.fragments.FragmentRepository;
import com.mellarius.fragments.FragmentRepository.RevisionEdit;
import com.mellarius.fragments.FragmentRepository.RevisionSelectorFragment;
import android.app.Activity;
import android.content.Context;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.SlidingDrawer;
import android.widget.TextView;

@SuppressWarnings("deprecation")
public class RevisionAdaptor extends ArrayAdapter<RevisionsHlp> {

	List<RevisionsHlp> inteventions = new ArrayList<RevisionsHlp>();
	List<PlanetViewHolder> cbList = new ArrayList<PlanetViewHolder>();
	Context context;
	Button button;
	SlidingDrawer sliding;
	View convertView;
	private FragmentManager fragManager;
	private FragmentTransaction transaction;
	int size;

	public RevisionAdaptor(Context context, List<RevisionsHlp> resource, FragmentManager fragManager) {
		super(context, R.layout.listview_checkbox, resource);
		// TODO Auto-generated constructor stub
		this.context = context;
		this.fragManager = fragManager;
		this.inteventions = resource;

	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		RevisionsHlp intevention = (RevisionsHlp) this.getItem(position);
		TextView name;
		TextView date;
		CheckBox cb;
		if (convertView == null) {
			LayoutInflater inflater = ((Activity) context).getLayoutInflater();
			convertView = inflater.inflate(R.layout.inner_fragment_interventionlist, parent, false);
			name = (TextView) convertView.findViewById(R.id.typeIntervention);
			date = (TextView) convertView.findViewById(R.id.dataIntervention);
			cb = (CheckBox) convertView.findViewById(R.id.checkBoxintervention);
			convertView.setTag(new PlanetViewHolder(name, date, cb));
			button = (Button) parent.getRootView().findViewById(R.id.revisionAction);
			sliding =  (SlidingDrawer) parent.getRootView().findViewById(R.id.slidingDrawerIntervention);
			cb.setOnClickListener(new View.OnClickListener() {
				public void onClick(View v) {
					CheckBox cb = (CheckBox) v;
					RevisionsHlp intevention = (RevisionsHlp) cb.getTag();
					if (cb.isChecked()) {
						RevisionSelectorFragment.clearAllChecBox();
						intevention.setChecked(cb.isChecked());
						transaction = fragManager.beginTransaction();
						transaction.replace(R.id.revisionOperationsFrame, new FragmentRepository.RevisionEdit());
						RevisionEdit.setType(true);
						transaction.addToBackStack(null);
						transaction.commit();
						sliding.toggle();
					} else {
						intevention.setChecked(false);
					}
				}
			});

		} else {
			// Because we use a ViewHolder, we avoid having to call
			// findViewById().
			PlanetViewHolder viewHolder = (PlanetViewHolder) convertView.getTag();
			cb = viewHolder.getCheckBox();
			name = viewHolder.getTextViewName();
			date = viewHolder.getTextViewDate();

		}
		// Tag the CheckBox with the Hive it is displaying, so that we can
		// access the hive in onClick() when the CheckBox is toggled.
		cb.setTag(intevention);
		// Display hive data
		name.setText(intevention.getIntervention());
		cb.setChecked(intevention.isChecked());
		if(intevention.getDate().length()>8){
			date.setText(intevention.getDate().substring(0, 9));	
		}
		

		return convertView;
	}

	/** Holds child views for one row. */
	private static class PlanetViewHolder {
		private CheckBox checkBox;
		private TextView name;
		private TextView date;

		public PlanetViewHolder(TextView name, TextView date, CheckBox checkBox) {
			this.checkBox = checkBox;
			this.name = name;
			this.date = date;
		}

		public CheckBox getCheckBox() {
			return checkBox;
		}

		public TextView getTextViewDate() {
			return date;
		}

		public TextView getTextViewName() {
			return name;
		}

	}

}
